package com.booking.springrestful.controller;


import com.booking.springrestful.domain.Users;
import com.booking.springrestful.resp.ResponseResult;
import com.booking.springrestful.service.UsersService;
import com.booking.springrestful.utills.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
@Slf4j
public class userController {

    @Resource
    private UsersService usersService;

    @GetMapping
    private ResponseResult<Users> getUser(HttpServletRequest request){
        Users user=usersService.getUser(request);
        return ResponseResult.success(user);
    }

    @PostMapping("/login")
    private ResponseResult userLogin(@RequestBody Users users){
        log.info(users.toString());
        Users user = usersService.userLogin(users.getUsername(), users.getPassword());
        String token = JwtUtil.createToken(user);
        return ResponseResult.success(token,"登录成功");

    }


    @PostMapping("/register")
    private ResponseResult userRegister(@RequestBody Users users){
        usersService.userRegister(users);
        return ResponseResult.success("注册成功");
    }


    //修改密码
    @PostMapping("/changePass")
    private ResponseResult changePass(HttpServletRequest request,String currentPassword,String newPassword){
        usersService.changePass(request.getAttribute("userName").toString(),currentPassword,newPassword);
        return ResponseResult.success("修改成功");
    }

    //修改个人信息
    @PostMapping("/update")
    private ResponseResult userUpdate(HttpServletRequest request,@RequestBody Users user){
        usersService.userUpdate(request.getAttribute("userName").toString(),user);
        return ResponseResult.success("修改成功");
    }



    //弹出成功的通知
    @PostMapping("/conSuccess")
    private ResponseResult conSuccess(HttpServletRequest request){
        String userName = request.getAttribute("userName").toString();
        Boolean flag=usersService.conSuccess(userName);
        log.info("弹出成功");
        return ResponseResult.success(flag);
    }


    //根据userid获取咨询师id
    @GetMapping("/getCouId")
    private ResponseResult getCouId(Integer userid){
        Integer id=usersService.getCouId(userid);
        return ResponseResult.success(id);
    }




}
